package com.example.figurehecheng.controller.merchant;


import com.example.figurehecheng.common.result.Result;
import com.example.figurehecheng.common.utils.AliOssUtil;
import com.example.figurehecheng.domain.dto.SaveImgDTO;
import com.example.figurehecheng.domain.entity.Image;
import com.example.figurehecheng.domain.vo.ImageVO;
import com.example.figurehecheng.service.IImageService;
import com.example.figurehecheng.service.IMerchantService;
import com.example.figurehecheng.service.impl.ImageServiceImpl;
import com.example.figurehecheng.service.impl.TourtableServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@RestController("merchantImageController")
@RequestMapping("/merchant/image")
@CrossOrigin
@Slf4j
@RequiredArgsConstructor
public class ImageController {

    private final IImageService iImageService;


    private  final ImageServiceImpl imageService;
    private final AliOssUtil aliOssUtil;
    private final TourtableServiceImpl tourtableService;


    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("上传");
        if (file == null || file.isEmpty()) {
            log.error("上传的文件为空");
//            return Result.error("上传的文件为空");
        }
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf("."));
        String fileName= UUID.randomUUID().toString()+suffix;

        String uploadResult = aliOssUtil.upload(file.getBytes(), fileName);
        return Result.success(uploadResult);
    }


    /**
     * 查询
     */
    @GetMapping("/selectimg")
    @ApiOperation(value = "查询图片")
    public Result<List> querySearch(){
        List<ImageVO> imglist = imageService.selectimg();
        System.out.println(imglist);
        return Result.success(imglist);
    }

    @DeleteMapping("/deleteimg")
    @ApiOperation("删除图片")
    public void deleteCoupon(@RequestParam("id") Integer id) {

        System.out.println("上早吧"+id);
      String urlname = imageService.sebyid(id);

        String replace = urlname.replace("https://figure-hecheng.oss-cn-shanghai.aliyuncs.com/", "");

        aliOssUtil.delete(replace);
        imageService.removeById(id);
    }

    @ApiOperation("新曾图片")
    @PostMapping("/addimage")
    public void  addimage(@RequestParam(value = "image") String image){
            imageService.saveimg(image);
    }



    @ApiOperation("新曾视频")
    @PostMapping("/addvoido")
    public void  addvoideo(@RequestParam(value="voido") String voido){
        System.out.println("上早吧"+voido);
        String sevoideo = tourtableService.sevoideo();
        if (!sevoideo.isEmpty()){
            String replace = sevoideo.replace("https://figure-hecheng.oss-cn-shanghai.aliyuncs.com/", "");
            aliOssUtil.delete(replace);
        }

        tourtableService.addvoideo(voido);

    }

    /**
     * 根据项目id和商家id查询图片
     * @param programId
     * @return
     */
    @GetMapping("/selectHouseImg")
    @ApiOperation("根据项目id和商家id查询图片")
    public Result<List<Image>> selectHouseImg(@RequestParam(value = "id") Integer programId){
        log.info("根据项目id和商家id查询图片：{}", programId);
        List<Image> imageList = iImageService.selectHouseImg(programId);
        return Result.success(imageList);
    }

    /**
     * 新增房间图片
     * @param
     * @return
     */
    @PostMapping("/saveHouseImg")
    @ApiOperation("新增房间图片")
    public Result saveHouseImg(@RequestBody SaveImgDTO saveImgDTO){
        log.info("新增房间图片：{}", saveImgDTO);
        iImageService.saveHouseImg(saveImgDTO);
        return Result.success();
    }

    /**
     * 删除房间图片
     * @param saveImgDTO
     * @return
     */
    @DeleteMapping("/deleteHouseImg")
    @ApiOperation("删除房间图片")
    public Result deleteHouseImg(@RequestBody SaveImgDTO saveImgDTO){
        log.info("删除房间图片：{}", saveImgDTO);
        iImageService.deleteHouseImg(saveImgDTO.getPhotograph());
        return Result.success();
    }



}
